package com.hc.acoustics.ui.activity

import android.app.AlertDialog
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.bean.contact.MainType
import com.hc.acoustics.bmob.BmobHelper
import com.hc.acoustics.bmob.BmobVersionBean
import com.hc.acoustics.bmob.IBmobNet
import com.hc.acoustics.rta.AudioMaker
import com.hc.acoustics.rta.PcmRecorderActivity
import com.hc.acoustics.widget.ShowImageDialog
import com.hc.acoustics.widget.banner.YcBannerBeanNew
import com.yc.yclibrary.exception.ApiException
import com.yc.yclibx.adapter.YcAdapterHelper
import com.yc.yclibx.adapter.YcRecyclerViewAdapter
import com.yc.yclibx.comment.YcUtilVersion
import kotlinx.android.synthetic.main.main_activity.*

class MainActivity : BaseActivity() {
    lateinit var mAdapter: YcRecyclerViewAdapter<Int>
    var mDialog: Dialog? = null
    var mShowImageDialog: ShowImageDialog? = null
    val mShowDialogImg = listOf(
        R.drawable.banner_1_2,
        R.drawable.banner_2_2,
        R.drawable.banner_3_2,
        R.drawable.banner_4_2,
        R.drawable.banner_5_2
    )
    val mShowDialogMsg = listOf(
        "点击复制公众号",
        "点击复制淘宝店链接",
        "点击复制抖音号",
        "点击复制qq群号",
        "点击复制微信号"
    )
    val mShowDialogCopy = listOf(
        "声学公开课",
        "￥ag3K1eNgIYr￥",
        "791928401",
        "884317104",
        "acoustics-talk"
    )

    override fun getLayoutId(): Int {
        return R.layout.main_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        mAdapter = object : YcRecyclerViewAdapter<Int>(this, R.layout.main_item) {
            override fun onUpdate(helper: YcAdapterHelper, item: Int, position: Int) {
                helper.setImageResource(R.id.mainItemIv, item)
            }
        }
        mAdapter.setItemClickListener { _, _, position ->
            when (val state = position + 1) {
                MainType.State.Main_1 -> WebActivity.newInstance(activity, "声学公开课")
                MainType.State.Main_2 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_3 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_4 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_5 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_6 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_7 -> FormulaTypeActivity.newInstance(activity, state)
                MainType.State.Main_8 -> ReferenceTypeActivity.newInstance(activity, state)
                MainType.State.Main_9 -> PcmRecorderActivity.newInstance(activity)
            }
        }
        mAdapter.addAll(MainType.getMainDrawable())
        mainRv.adapter = mAdapter
        mainRv.layoutManager = GridLayoutManager(this, 3)
        bannerV.setOnItemClickListener { item, position ->
            if (mShowImageDialog == null) {
                mShowImageDialog = ShowImageDialog(activity)
            }
            mShowImageDialog!!.setImageResource(mShowDialogImg[position])
            mShowImageDialog!!.setCopyMsg(mShowDialogCopy[position])
            mShowImageDialog!!.setMessage(mShowDialogMsg[position])
            mShowImageDialog!!.show()
        }
        bannerV.build(
            listOf(
                YcBannerBeanNew.newRes(R.drawable.banner_1_1),
                YcBannerBeanNew.newRes(R.drawable.banner_2_1),
                YcBannerBeanNew.newRes(R.drawable.banner_3_1),
                YcBannerBeanNew.newRes(R.drawable.banner_4_1),
                YcBannerBeanNew.newRes(R.drawable.banner_5_1)
            )
        )
    }

    private fun checkVersion() {
        BmobHelper.getVersionData(object : IBmobNet<BmobVersionBean> {
            override fun success(t: BmobVersionBean?) {
                if (t != null && (t.versionCode > YcUtilVersion.getVersionCode())) {//是否需要更新
                    if (mDialog == null) {
                        mDialog = AlertDialog.Builder(activity).setTitle("发现新版本")
                            .setPositiveButton("下载") { dialogInterface, _ ->
                                dialogInterface.dismiss()
                                val intent = Intent()
                                intent.action = "android.intent.action.VIEW"
                                intent.data = Uri.parse(t.versionUrl)
                                startActivity(intent)
                            }
                            .setNegativeButton("取消") { dialogInterface, _ -> dialogInterface.dismiss() }
                            .create()
                    }
                    mDialog?.show()
                } else {
                    showToast("已是最新版本！~")
                }
            }

            override fun fail(e: ApiException?) {
                showToast("获取版本信息失败！")
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        mDialog?.dismiss()
        bannerV.destroy()
        mShowImageDialog?.dismiss()
    }
}
